package com.tau.egonetwork.gui.core;

import java.util.HashMap;
import java.util.Set;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import com.tau.egonetwork.gui.Environment;
import com.tau.egonetwork.profiles.ProfileFile;
import com.tau.egonetwork.profiles.ProfileFile.SocialNetworks;

public class FileOpenDialog extends BasicDialog {
	
	private static final String KEY_FILENAME = "filename";
	
	private SocialNetworks network;
	
	private String filename = null;
	
	private Table profilesTable;
		
	public FileOpenDialog(Shell parent, SocialNetworks net) {
		super(parent);
		this.network = net;
	}

	public FileOpenDialog(Shell parent) {
		super(parent);
		this.network = null; //don't look for specific
		setText("Open Profile");
	}
	
	@Override
	protected Object setReturnValue() {
		return filename;
	}
	
	@Override
	protected void createWindowContents() {

		Composite windowComposite = new Composite(shell, SWT.NONE);
		GridLayout windowCompositeLayout = new GridLayout(3, false);
		windowCompositeLayout.marginWidth = 0;
		windowCompositeLayout.marginHeight = 0;
		windowCompositeLayout.marginLeft = 0;
		windowCompositeLayout.marginRight = 0;
		windowCompositeLayout.marginTop = 0;
		windowCompositeLayout.marginBottom = 0;
		windowComposite.setLayout(windowCompositeLayout);
		windowComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		profilesTable = new Table(windowComposite, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION);
		GridData profilesTableGridData = new GridData(SWT.FILL, SWT.FILL, true, true);
		profilesTableGridData.heightHint = 380;
		profilesTableGridData.widthHint = 320;
		profilesTableGridData.horizontalSpan = 3;
		profilesTable.setLayoutData(profilesTableGridData);
	
		profilesTable.setLinesVisible(false);
		profilesTable.setHeaderVisible(false);
		
		TableColumn column = new TableColumn(profilesTable, SWT.NONE);
		column.setResizable(false);
		
		final Text searchText = new Text(windowComposite, SWT.BORDER | SWT.FLAT);
		searchText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		searchText.setTextLimit(50);
		searchText.setFocus();
		
		final Button searchButton = new Button(windowComposite, SWT.PUSH | SWT.FLAT);
		searchButton.setImage(Environment.ICON_SEARCH);
		searchButton.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
		searchButton.setEnabled(false);
		
		final Button clearButton = new Button(windowComposite, SWT.PUSH | SWT.FLAT);
		clearButton.setText("Clear");
		clearButton.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
		clearButton.setEnabled(false);
		
		boolean noProfiles = true;
		
		final HashMap<String, String> profiles = ProfileFile.getAllProfileFiles(network);
		if (profiles != null) {
			if (profiles.size() > 0) {
				noProfiles = false; 
				buildAndPopulateTable(profilesTable, profiles);
			}
		}
		
		if (noProfiles) {
			EgoNetworkMessageBox mbox = new EgoNetworkMessageBox(shell, SWT.ICON_INFORMATION, SWT.OK);
			mbox.setText("Open Profile");
			mbox.setMessage("There are no saved Profiles");
			mbox.open();
			shell.close();
			setDoNotOpenDialog();
			return;
		}
		
		searchText.addListener(SWT.KeyUp, new Listener() {

			@Override
			public void handleEvent(Event event) {
				searchButton.setEnabled(!searchText.getText().trim().equals(""));
			}
			
		});
		
		searchButton.addListener(SWT.Selection, new Listener() {

			@Override
			public void handleEvent(Event event) {
				HashMap<String, String> searchResults = new HashMap<String, String>();
				Set<String> originalKeys = profiles.keySet();
				for (String key : originalKeys) {
					String val = profiles.get(key);
					if (val.toLowerCase().startsWith(searchText.getText().toLowerCase())) {
						searchResults.put(key, val);
					}
				}
				buildAndPopulateTable(profilesTable, searchResults);
				searchText.setFocus();
				clearButton.setEnabled(true);
				setOKButtonEnabled(false);
			}
			
		});
		
		profilesTable.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseUp(MouseEvent e) {
				setOKButtonEnabled(profilesTable.getSelectionCount() == 1);
			}
			
			@Override
			public void mouseDown(MouseEvent e) {
				//nothing to do here
			}
			
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				onOKClicked();
			}
		});
		
		clearButton.addListener(SWT.Selection, new Listener() {

			@Override
			public void handleEvent(Event event) {
				buildAndPopulateTable(profilesTable, profiles);
				clearButton.setEnabled(false);
				searchButton.setEnabled(false);
				searchText.setText("");
				searchText.setFocus();
				setOKButtonEnabled(false);
			}
			
		});
		
	}
	
	private void buildAndPopulateTable(Table profilesTable, HashMap<String, String> data) {

		if (profilesTable.getItemCount() > 0) {
			profilesTable.remove(0 , profilesTable.getItemCount() - 1);
		}
		
		Set<String> keys = data.keySet();
					
		for (String key : keys) {
			
			TableItem item = new TableItem(profilesTable, SWT.NONE);
			if (key.startsWith(SocialNetworks.Facebook.toString())) {
				item.setImage(0, Environment.ICON_FACEBOOK_PROFILE);
			} else if (key.startsWith(SocialNetworks.Linkedin.toString())) {
				item.setImage(0, Environment.ICON_LINKEDIN_PROFILE);
			}
			item.setText(0, data.get(key));
			item.setData(KEY_FILENAME, key);

		}

		profilesTable.getColumn(0).pack();
		
	}

	@Override
	protected void onOKClicked() {
		int selectedItem = profilesTable.getSelectionIndex();
		if (selectedItem >= 0) {
			filename = profilesTable.getItem(selectedItem).getData(KEY_FILENAME) + "";
		}
		shell.close();
	}
	
}
